

package com.wind.sqlite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wind.sqlite.common.base.BaseModelPageDTO;
import com.wind.sqlite.common.utils.PageUtils;
import com.wind.sqlite.common.utils.Query;
import com.wind.sqlite.dao.SysUserDao;
import com.wind.sqlite.entity.SysUserEntity;
import com.wind.sqlite.service.SysUserService;
import com.wind.sqlite.common.base.BaseModelPageDTO;
import com.wind.sqlite.common.utils.PageUtils;
import com.wind.sqlite.common.utils.Query;
import com.wind.sqlite.dao.SysUserDao;
import com.wind.sqlite.entity.SysUserEntity;
import com.wind.sqlite.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String) params.get("username");
        Long createUserId = (Long) params.get("createUserId");

        IPage<SysUserEntity> page = this.page(
                new Query<SysUserEntity>().getPage(params),
                new QueryWrapper<SysUserEntity>()
                        .like(StringUtils.isNotBlank(username), "username", username)
                        .eq(createUserId != null, "create_user_id", createUserId)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(BaseModelPageDTO<SysUserEntity> data) {
        SysUserEntity entity = data.getData();
        QueryWrapper<SysUserEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<SysUserEntity>();
        }else{
            queryWrapper = new QueryWrapper<SysUserEntity>();
            queryWrapper = getSysUserEntityQueryWrapper(entity, queryWrapper);
        }
        IPage<SysUserEntity> page = this.page(
                new Query<SysUserEntity>().getPage(data),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public SysUserEntity queryOne(SysUserEntity entity) {
        QueryWrapper<SysUserEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<SysUserEntity>();
        }else{
            queryWrapper = new QueryWrapper<SysUserEntity>();
            queryWrapper = getSysUserEntityQueryWrapper(entity, queryWrapper);
        }

        return baseMapper.selectOne(queryWrapper);
    }


    private QueryWrapper<SysUserEntity> getSysUserEntityQueryWrapper(SysUserEntity entity, QueryWrapper<SysUserEntity> queryWrapper) {
        if(StringUtils.isNotEmpty(entity.getUsername())){
            queryWrapper=queryWrapper.eq("username", entity.getUsername());
        }
        return queryWrapper;
    }



    @Override
    @Transactional
    public void saveUser(SysUserEntity user) {

        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
        user.setSalt(salt);
        this.save(user);


    }

    @Override
    @Transactional
    public void update(SysUserEntity user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
        }
        this.updateById(user);


    }

    @Override
    public void deleteBatch(Long[] userId) {
        this.removeByIds(Arrays.asList(userId));
    }

    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setPassword(newPassword);
        return this.update(userEntity,
                new QueryWrapper<SysUserEntity>().eq("id", userId).eq("password", password));
    }


}